<template>
  <transition appear name="fade">
    <div class="private_letter" v-drag>
      <div class="nav">
        <span>私信：{{user.name}}</span>
        <off-btn @off="off"></off-btn>
      </div>
      <div class="content" @mousedown.stop>
        <span>内容：</span>
        <textarea class="text" maxlength="100" placeholder="(100字以内)" v-model="content"></textarea>
        <span class="btn" @click="send"> 
          <svg class='icon' aria-hidden='true'>
            <use xlink:href='#icon-SEND'></use>
          </svg>
          发送
        </span>
      </div>
    </div>
  </transition>
</template>

<script>
import { mapActions } from 'vuex';
import offBtn from "../../info/offBtn.vue"
export default {
  name: 'Privateletter',

  data() {
    return {
      content: ''
    };
  },

  props: {
    user: Object
  },

  mounted() {
    
  },

  methods: {
    off(){
      this.$parent.user = null
    },
    send(){
      if(this.content.trim() == ''){
        this.showHint({title:'请不要发送空白消息!',isFlag:false})
        return
      }
      this.$api.sendLetter({
        username: this.user.username,
        text: this.content
      }).then(res => {
        if(res == 1){
          this.showHint({title:'已发送!',isFlag:true})
          this.off()
        }else{
          this.showHint({title:res,isFlag:false})
        }
      })
    },
    ...mapActions(['showHint'])
  },

  components: {
    offBtn
  }
};
</script>

<style scoped>
.private_letter{
  width: 320px;
  background-color: white;
  border: 1px solid gray;
  box-shadow: 0 0 5px gray;
  transform: translate(-50%,-50%);
  top: 50%;
  left: 50%;
}
.nav{
  height: 30px;
  line-height: 30px;
  background-color: gray;
  color: white;
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
}
.content{
  padding: 10px 30px;
}
.content .text{
  display: block;
  width: 100%;
  outline: none;
  height: 120px;
  font-family: '微软雅黑';
  resize: none;
  text-indent: 2em;
  transition: all .3s ease;
  border: 1px solid gray;
}
.content .text:focus{
  border: 1px solid rgb(41, 152, 255);
}
.btn{
  display: block;
  background-color: green;
  color: white;
  cursor: pointer;
  padding: 4px 10px;
  text-align: center;
  margin-top: 10px;
}
</style>